<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />    
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>
    <header class="header header-f" style="padding-top:2rem; height:4rem;">
        <div class="header-l aui-pull-left" onclick="closeWin()">
            <i class="fa fa-angle-left"></i>
        </div>
        <div class="header-title">
            新建收货地址
        </div>
        <div class="header-r aui-pull-right">
        </div>
    </header>
    <div style="height:4rem;"></div>

    <div class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-list-in">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        姓名：
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请填写收货人姓名" id="name">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        手机号：
                    </div>
                    <div class="aui-list-item-input">
                        <input type="number" placeholder="请填写联系电话" id="tel" maxlength="11">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner" id="addressView">
                    <div class="aui-list-item-label">
                        所在地区：
                    </div>
                    <div class="aui-list-item-input" id="city">北京市 北京市 东城区
                        <!-- <input type="text" placeholder="所在地区"> -->
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        详细地址：
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="街道，楼牌号等" id="address">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        设置默认地址
                    </div>
                    <div class="aui-list-item-input">
                        <input type="checkbox" class="aui-switch aui-pull-right" checked="checked" id="default" onclick="changeChecked();">
                    </div>
                </div>
            </li>
        </ul>
    </div>

    <div class="aui-content-padded">
        <div class="aui-btn aui-btn-danger aui-btn-block aui-btn-sm" onclick="addAddress();">保存地址</div>
    </div>


</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript">

    apiready = function(){
        var addressView = api.require('addressView');
        addressView.open({
        file_addr: 'widget://res/district.txt', //数据源地址
        selected_color: '#ff0000', //颜色
        pro_id: 110000, //省id
        city_id: 110100, //市id
        dir_id: 110101 //区id
    });
        $api.addEvt($api.byId("addressView"), 'click', function() {
            addressView.show({}, function(ret, err) {
                if (ret.status) {
                // alert(JSON.stringify(ret)); //返回值
                var data = ret.data;
                proName = data[0].name;
                cityName = data[1].name;
                dirName = data[2].name;
                var address = proName + ' ' + cityName + ' ' + dirName;
                alert(address);
                $('#city').html(address);
            }
        });
        });
    }

    function addAddress(){

        var isDefault = $('#default').attr("checked");
        var defaultValue = 0;
        if (isDefault) {
            defaultValue = 1;
        }else{
            defaultValue = 0;
        }

        var name = $api.byId('name').value;
        var tel = $api.byId('tel').value;
        var address = $api.byId('address').value;

        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/Address/add',
            method: 'post',
            data: {
                values: {
                    name: name,
                    tel: tel,
                    province: proName,
                    city: cityName,
                    region: dirName,
                    address: address,
                    default: defaultValue,
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                api.toast({
                    msg: ret.message,
                    duration: 2000,
                    location: 'bottom',
                    global: 'true',
                });
                api.sendEvent({
                    name: 'reload'
                });
                api.closeWin();
            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });

    }

    // 改变状态(是否为默认地址)
    function changeChecked(){
        var isDefault = $('#default').attr("checked");

        if (isDefault) {
            $('#default').attr('checked',null);
        }else{
            $('#default').attr('checked','checked'); 
        }
    }

    function closeWin(){
        api.closeWin({
        });
    }
</script>
</html>